// 定义主题配置
export const themes = [
  {
    name: 'default',
    label: '天空蓝',
    colors: {
      primary: '#6E8CD5',
      secondary: '#89C4F4',
      background: 'rgba(240, 245, 255, 0.8)',
      headerBg: 'linear-gradient(160deg, #89C4F4 0%, #6E8CD5 100%)',
      cardBg: '#FFFFFF'
    }
  },
  {
    name: 'lavender',
    label: '薰衣草',
    colors: {
      primary: '#B5A8E0',
      secondary: '#C9BFF0',
      background: 'rgba(245, 240, 255, 0.8)',
      headerBg: 'linear-gradient(160deg, #C9BFF0 0%, #B5A8E0 100%)',
      cardBg: '#FFFFFF'
    }
  },
  {
    name: 'wechat',
    label: '微信绿',
    colors: {
      primary: '#07C160',
      secondary: '#95EC9C',
      background: 'rgba(240, 255, 245, 0.8)',
      headerBg: 'linear-gradient(160deg, #95EC9C 0%, #07C160 100%)',
      cardBg: '#FFFFFF'
    }
  },
  {
    name: 'alipay',
    label: '支付宝',
    colors: {
      primary: '#1677FF',
      secondary: '#69B5FF',
      background: 'rgba(240, 248, 255, 0.8)',
      headerBg: 'linear-gradient(160deg, #69B5FF 0%, #1677FF 100%)',
      cardBg: '#FFFFFF'
    }
  },
  {
    name: 'bilibili',
    label: '哔哩粉',
    colors: {
      primary: '#FB7299',
      secondary: '#FFB5C8',
      background: 'rgba(255, 240, 245, 0.8)',
      headerBg: 'linear-gradient(160deg, #FFB5C8 0%, #FB7299 100%)',
      cardBg: '#FFFFFF'
    }
  },
  {
    name: 'peach',
    label: '蜜桃粉',
    colors: {
      primary: '#F5B6B6',
      secondary: '#FFD0D0',
      background: 'rgba(255, 245, 245, 0.8)',
      headerBg: 'linear-gradient(160deg, #FFD0D0 0%, #F5B6B6 100%)',
      cardBg: '#FFFFFF'
    }
  },
  {
    name: 'douyin',
    label: '抖音红',
    colors: {
      primary: '#FE2C55',
      secondary: '#FF8399',
      background: 'rgba(255, 242, 245, 0.8)',
      headerBg: 'linear-gradient(160deg, #FF8399 0%, #FE2C55 100%)',
      cardBg: '#FFFFFF'
    }
  },
  {
    name: 'qq',
    label: 'QQ蓝',
    colors: {
      primary: '#12B7F5',
      secondary: '#66D3FF',
      background: 'rgba(240, 250, 255, 0.8)',
      headerBg: 'linear-gradient(160deg, #66D3FF 0%, #12B7F5 100%)',
      cardBg: '#FFFFFF'
    }
  },
  {
    name: 'sunset',
    label: '日落橙',
    colors: {
      primary: '#FFB182',
      secondary: '#FFCEB4',
      background: 'rgba(255, 248, 240, 0.8)',
      headerBg: 'linear-gradient(160deg, #FFCEB4 0%, #FFB182 100%)',
      cardBg: '#FFFFFF'
    }
  },
  {
    name: 'sage',
    label: '鼠尾草',
    colors: {
      primary: '#9BAF9F',
      secondary: '#C5D1C8',
      background: 'rgba(245, 248, 245, 0.8)',
      headerBg: 'linear-gradient(160deg, #C5D1C8 0%, #9BAF9F 100%)',
      cardBg: '#FFFFFF'
    }
  },
  {
    name: 'ocean',
    label: '海洋蓝',
    colors: {
      primary: '#64B5CD',
      secondary: '#92D3E5',
      background: 'rgba(240, 250, 255, 0.8)',
      headerBg: 'linear-gradient(160deg, #92D3E5 0%, #64B5CD 100%)',
      cardBg: '#FFFFFF'
    }
  },
  {
    name: 'olive',
    label: '橄榄绿',
    colors: {
      primary: '#A5B995',
      secondary: '#C7D5BB',
      background: 'rgba(245, 250, 245, 0.8)',
      headerBg: 'linear-gradient(160deg, #C7D5BB 0%, #A5B995 100%)',
      cardBg: '#FFFFFF'
    }
  },
  {
    name: 'mauve',
    label: '木槿紫',
    colors: {
      primary: '#B784A7',
      secondary: '#D4B0C6',
      background: 'rgba(250, 245, 250, 0.8)',
      headerBg: 'linear-gradient(160deg, #D4B0C6 0%, #B784A7 100%)',
      cardBg: '#FFFFFF'
    }
  },
  {
    name: 'xiaohongshu',
    label: '小红书',
    colors: {
      primary: '#FE2C55',
      secondary: '#FF6B91',
      background: 'rgba(255, 242, 245, 0.8)',
      headerBg: 'linear-gradient(160deg, #FF6B91 0%, #FE2C55 100%)',
      cardBg: '#FFFFFF'
    }
  },
  {
    name: 'zhihu',
    label: '知乎蓝',
    colors: {
      primary: '#0066FF',
      secondary: '#66B3FF',
      background: 'rgba(240, 245, 255, 0.8)',
      headerBg: 'linear-gradient(160deg, #66B3FF 0%, #0066FF 100%)',
      cardBg: '#FFFFFF'
    }
  },
  {
    name: 'netease',
    label: '网易红',
    colors: {
      primary: '#D33A31',
      secondary: '#FF7875',
      background: 'rgba(255, 242, 242, 0.8)',
      headerBg: 'linear-gradient(160deg, #FF7875 0%, #D33A31 100%)',
      cardBg: '#FFFFFF'
    }
  }
]

// 主题管理类
export class ThemeManager {
  constructor() {
    this.currentTheme = 'default' // 保持这个不变，作为fallback
    this.initialTheme = uni.getStorageSync('currentTheme')
    if (!this.initialTheme) {
      // 如果是首次使用，设置默认主题为天空蓝并保存
      this.initialTheme = 'default'
      this.saveTheme('default')
      
      // 同时保存主题颜色
      const defaultTheme = themes.find(t => t.name === 'default')
      if (defaultTheme) {
        uni.setStorageSync('themeColors', defaultTheme.colors)
      }
    }
    const theme = themes.find(t => t.name === this.initialTheme)
    if (theme) {
      this.currentColors = theme.colors
    }
  }

  // 切换主题
  switchTheme(themeName) {
    const theme = themes.find(t => t.name === themeName)
    if (!theme) {
      return
    }

    this.currentTheme = themeName
    this.currentColors = theme.colors
    
    // 立即更新当前页面样式
    const pages = getCurrentPages()
    const currentPage = pages[pages.length - 1]
    if (currentPage && currentPage.$vm) {
      currentPage.$vm.currentThemeStyle = {
        '--primary': theme.colors.primary,
        '--secondary': theme.colors.secondary,
        '--background': theme.colors.background,
        '--headerBg': theme.colors.headerBg,
        '--cardBg': theme.colors.cardBg
      }
      // 强制更新视图
      currentPage.$vm.$forceUpdate()
    }
    
    // 更新全局样式变量
    uni.setStorageSync('themeColors', theme.colors)
    // 触发页面更新
    getApp().globalData.themeColors = theme.colors
    
    // 立即应用主题样式
    uni.$emit('themeChanged', theme.colors)
    this.saveTheme(themeName)
  }

  // 获取当前主题颜色
  getCurrentThemeColors() {
    return this.currentColors || themes[0].colors
  }

  // 保存主题设置
  saveTheme(themeName) {
    uni.setStorageSync('currentTheme', themeName)
  }

  // 加载保存的主题
  loadSavedTheme() {
    if (this.initialTheme !== this.currentTheme) {
      this.switchTheme(this.initialTheme)
    }
  }
} 